


function answer = answerDialog(window, question, responseAlternatives, fontsize)
    % present simplistic fake modal dialog box displaying a question buttons with responseAlternatives
    % collect mouse click responses (using clickMouseOnTargets)
    %
    % input
    %    window: a ptb windowPtr
    %    question: a string containing the question
    %    responseAlternatives: a cell array containing a string for each response button label
    %    [fontsize]: font size
    % output:
    %     answer contains which button was clicked (as index into responseAlternatives)
    % dependencies (nonstandard):
    %     depends on clickMouseOnTargets, which is distributed separately because it may be useful on its own
    % author:
    %     jochen laubrock

 white=whiteindex(window);
    black=blackindex(window);



    if nargin < 3
        error('anwerDialog: you need to specify at least window, question, and responseAlternatives');
    end
    if nargin == 3
        fontsize = 20;
    end

    % minimum x and y space between button text and button border
    BUTTON_WHITESPACE_X = round(.5*fontsize);
    BUTTON_WHITESPACE_Y = round(.5*fontsize);

    fgColor = [ 20 100 255  ] %screen('TextColor', window)
    bgColor = [100 100 3 ] %screen('TextBackgroundColor', window)
    grayColor = round((fgColor(1:3) + bgColor(1:3))*0.75);

    screenRect = Screen('Rect', window);
    oldTextSize = Screen('TextSize', window , fontsize);

    % ====================================
    % = calculate position of some rects =
    % ====================================

    [maxResponseLtrs, maxResponseLtrsIx] = max(cellfun(@length, responseAlternatives));
    [normBoundsRect, offsetBoundsRect] = Screen('TextBounds', window, responseAlternatives{maxResponseLtrsIx});
    [minButtonWidth, minButtonHeight] = RectSize(normBoundsRect);

    nButtons = length(responseAlternatives);
    buttonHeight = minButtonHeight + 2*BUTTON_WHITESPACE_Y;
    buttonWidth = minButtonWidth + 2*BUTTON_WHITESPACE_X;
    buttonRect = SetRect(0, 0, buttonWidth, buttonHeight);
    buttonHorizSep = round(buttonWidth)/(nButtons+1);
    buttonBoundsWidth = nButtons*buttonWidth + (nButtons-1)*buttonHorizSep ;
    buttonBoundsRect = SetRect( 0, 0, buttonBoundsWidth, buttonHeight);

    buttonRect = [0 0 buttonWidth buttonHeight]
    buttonRowRect = [0 0 nButtons*buttonWidth + (nButtons-1)*buttonHorizSep buttonHeight];

    [questionBoundsRect, offsetBoundsRect] = Screen('TextBounds', window, question);

    tmpRect = [0 0 max(RectWidth(buttonRowRect), RectWidth(questionBoundsRect)), RectHeight(buttonRowRect)];
    dialogRect = ScaleRect(tmpRect, 1.3, 4);
    assert(RectWidth(dialogRect) < RectWidth(screenRect)); % make sure it fits on screen
    dialogRect  = CenterRect(dialogRect, screenRect);

    [x,y] = RectCenter(dialogRect);
    buttonRowRect = CenterRectOnPoint(buttonRowRect, x, dialogRect(RectBottom) - 1.5*buttonHeight)

    cellRects=ArrangeRects(nButtons, buttonRect, buttonRowRect)
    answerRects = nan*ones(nButtons,4);
    for i=1:nButtons
        answerRects(i,:) = CenterRect(buttonRect, cellRects(i,:));
        [normBoundsRect, offsetBoundsRect]= Screen('TextBounds', window, responseAlternatives{i});
        answTxtRects(i,:) = CenterRect(normBoundsRect, answerRects(i,:));
    end

    questionRect = AlignRect(questionBoundsRect, dialogRect, 'center', 'top');
    tmpRect = setRect(questionRect(RectLeft), questionRect(RectTop), questionRect(RectRight), buttonRowRect(RectBottom));
    questionRect = CenterRect(questionRect, tmpRect);

    % ================================
    % = draw dialog and get response =
    % ================================
    x = 0;
    y = 0;
    buttons = zeros(1,4);
    hit = 0;
    while (hit ~= 1)
        drawDialog(window, fgColor, dialogRect, answerRects, answTxtRects, questionRect, question, responseAlternatives)
        Screen('Flip', window);

        [answer, test]  = clickMouseOnTargets(window, answerRects);
        if answer > 0 && test==1
            answer1 = answer;
            drawDialog(window, fgColor, dialogRect, answerRects, answTxtRects, questionRect, question, responseAlternatives);
            answTxtRect = answTxtRects(answer1,:);
            Screen('FillRect', window, grayColor, InsetRect(answerRects(answer1,:), 0.5, 0.5));
            Screen('DrawText', window, responseAlternatives{answer}, answTxtRect(RectLeft), answTxtRect(RectTop));
            Screen('Flip', window);

            answer2 = -1;
            test = 0;
            while test == 0
                [answer2, test] = clickMouseOnTargets(window, answerRects, 0);
                if answer2 > 0 &&  answer2 ~= answer1
                    drawDialog(window, fgColor, dialogRect, answerRects, answTxtRects, questionRect, question, responseAlternatives);
                    Screen('Flip', window);
                end
            end
            hit = answer1 == answer2;
        end
    end

    % restore old state
    Screen('TextSize', window , oldTextSize);

    % done with answerDialog

    % ========================================================
    % = helper function to handle most of the actual drawing =
    % ========================================================
    % since timing is not critical here, these repeatedly called fragments are factored out into a separate function
    % pro: readability, maintainability
    % cons: potential function call overhead; ugly parameter list
    function drawDialog(window, fgColor, dialogRect, answerRects, answTxtRects, questionRect, question, responseAlternatives);
        Screen('FrameRect', window, fgColor, [dialogRect; answerRects]', [2 ones(1, size(answerRects, 1))]);
        Screen('DrawText', window, question, questionRect(RectLeft), questionRect(RectTop));
        for i=1:length(responseAlternatives)
            answTxtRect = answTxtRects(i,:);
            Screen('DrawText', window, responseAlternatives{i}, answTxtRect(RectLeft), answTxtRect(RectTop),black);
        end
    end


    return

end










